package com.lee.xcore.utils;

import android.content.Context;
import android.os.Parcelable;

import com.tencent.mmkv.MMKV;

import java.util.Collections;
import java.util.Set;

public class MMKVUtils {

    private MMKV mmkv;
    private static MMKVUtils instance;

    private MMKVUtils() {
    }

    public void initialize(Context context, String id) {
        MMKV.initialize(context);
        mmkv = MMKV.mmkvWithID(id);
    }

    public void initialize(Context context) {
        MMKV.initialize(context);
        mmkv = MMKV.defaultMMKV();
    }

    public static MMKVUtils getInstance() {
        if (instance == null) {
            synchronized (MMKVUtils.class) {
                if (instance == null) {
                    instance = new MMKVUtils();
                }
            }
        }
        return instance;
    }

    public void encode(String key, Object value) {
        checkNotInit();
        if (value == null) return;
        if (value instanceof String) {
            mmkv.encode(key, (String) value);
        } else if (value instanceof Integer) {
            mmkv.encode(key, ((Integer) value).intValue());
        } else if (value instanceof Boolean) {
            mmkv.encode(key, ((Boolean) value).booleanValue());
        } else if (value instanceof Float) {
            mmkv.encode(key, ((Float) value).floatValue());
        } else if (value instanceof Long) {
            mmkv.encode(key, ((Long) value).longValue());
        } else if (value instanceof Set) {
            mmkv.encode(key, (Set<String>) value);
        } else {
            mmkv.encode(key, (Set<String>) value);
        }

    }

    public <T extends Parcelable> void encode(String key, T value) {
        checkNotInit();
        if (value == null) return;
        mmkv.encode(key, value);
    }

    public void encode(String key, Set<String> value) {
        checkNotInit();
        if (value == null) return;
        mmkv.encode(key, value);
    }

    public int decodeInt(String key) {
        checkNotInit();
        return mmkv.decodeInt(key, 0);
    }

    public int decodeInt(String key, int defaultValue) {
        checkNotInit();
        return mmkv.decodeInt(key, defaultValue);
    }

    public double decodeDouble(String key) {
        checkNotInit();
        return mmkv.decodeDouble(key, 0);
    }

    public double decodeDouble(String key, double defaultValue) {
        checkNotInit();
        return mmkv.decodeDouble(key, defaultValue);
    }

    public long decodeLong(String key) {
        checkNotInit();
        return mmkv.decodeLong(key, 0L);
    }

    public long decodeLong(String key, long defaultValue) {
        checkNotInit();
        return mmkv.decodeLong(key, defaultValue);
    }

    public boolean decodeBoolean(String key) {
        checkNotInit();
        return mmkv.decodeBool(key, false);
    }

    public boolean decodeBoolean(String key, boolean defaultValue) {
        checkNotInit();
        return mmkv.decodeBool(key, defaultValue);
    }

    public float decodeFloat(String key) {
        checkNotInit();
        return mmkv.decodeFloat(key, 0F);
    }

    public float decodeFloat(String key, float defaultValue) {
        checkNotInit();
        return mmkv.decodeFloat(key, defaultValue);
    }

    public byte[] decodeByteArray(String key) {
        checkNotInit();
        return mmkv.decodeBytes(key);
    }

    public byte[] decodeByteArray(String key, byte[] defaultValue) {
        checkNotInit();
        return mmkv.decodeBytes(key, defaultValue);
    }

    public String decodeString(String key) {
        checkNotInit();
        return mmkv.decodeString(key, "");
    }

    public String decodeString(String key, String defaultValue) {
        checkNotInit();
        return mmkv.decodeString(key, defaultValue);
    }


    public <T extends Parcelable> T decodeParcelable(String key, Class<T> tClass) {
        checkNotInit();
        return mmkv.decodeParcelable(key, tClass);
    }

    public Set<String> decodeStringSet(String key) {
        checkNotInit();
        return mmkv.decodeStringSet(key, Collections.emptySet());
    }


    public void removeKey(String key) {
        checkNotInit();
        mmkv.removeValueForKey(key);
    }

    public void clearAll() {
        checkNotInit();
        mmkv.clearAll();
    }


    private void checkNotInit() {
        if (mmkv == null)
            throw new UnsupportedOperationException("MMKVUtils 未初始化");
    }

}
